package com.future.mayi.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.KeepGoodsModel;
import com.future.mayi.utils.ClickUtils;
import com.future.mayi.utils.HtmlFormat;
import com.future.mayi.utils.KefuUtils;
import com.future.mayi.utils.PriceUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 上色修复
 */
public class KeepGoodsInfoActivity extends BaseTitleActivity {
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.tv_money)
    TextView tvMoney;


    private KeepGoodsModel model;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("上色修复");
        getRightImg().setImageResource(R.drawable.bottom_kefu);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_keep_goods_info);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {


        if (getIntent() != null) {
            model = (KeepGoodsModel) getIntent().getSerializableExtra("model");
        }
        if (model == null) {
            finish();
            return;
        }
        getTitleText().setText(model.getType_name());
        final String img = model.getType_img();

        new HttpRequest<DefaultResponse>()
                .with(this)
                .addParam("type_id", model.getType_id())
                .setApiCode(ApiCst.getDetailByTypeID)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        if (TextUtils.isEmpty(response.content)) return;
                        model = App.getGson().fromJson(response.content, KeepGoodsModel.class);
                        if (model==null)return;
                        model.setType_img(img);
                        tvMoney.setText(PriceUtils.getPriceText(model.getPrice()));
                        webView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(model.getConfig()), "text/html", "utf-8", null);
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {

                    }
                })
                .start(new DefaultResponse());


    }

    @Override
    protected void initListener() {
        getRightImg().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KefuUtils.startActivity(KeepGoodsInfoActivity.this);
            }
        });
    }


    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        if (ClickUtils.isNoLogin(mContext, true)) return;
        Intent intent = new Intent(this, NewAppraisalOrderActivity.class);
        intent.putExtra("money", model.getPrice());
        intent.putExtra("keep", model);
        startActivity(intent);
    }


}
